package logic

import (
	"context"
	"fmt"
	"zc2/model/mysql"
	"zc2/model/redis"

	"zc2/userrpc/internal/svc"
	"zc2/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *userrpc.RegisterRequest) (*userrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get("sendsmsRegister" + in.Name)
	if get.Val() != in.Sendsms {
		return nil, fmt.Errorf("验证码错误，请重新输入")
	}
	var finduser mysql.User
	mysql.DB.Where("name=?", in.Name).Find(&finduser)
	if finduser.ID != 0 {
		return nil, fmt.Errorf("该用户已被注册，请查询输入")
	}
	user := mysql.User{
		Name:     in.Name,
		Password: in.Password,
	}
	mysql.DB.Create(&user)
	//redis.Client.Del("sendsmsRegister" + in.Name)
	return &userrpc.RegisterResponse{Id: int64(user.ID)}, nil
}
